﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using HashMaster.Lib;

namespace HashManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        const string ServiceUrl = @"http://stc.dofeel.cn:1456/";
        //const string ServiceUrl = @"http://localhost:1170/";

        public static Dictionary<EnumStatus, string> dictStatus = new Dictionary<EnumStatus, string>();

        public MainWindow()
        {
     
            InitializeComponent();
        }

        private void lstCatalog_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            

        }

        private void RefreshDataGrid()
        {
            dataDetails.ItemsSource =
                ServiceUtility.GetAllTasksDetail((chkMode.IsChecked == true ? "3" : "1"), ServiceUrl);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (EnumStatus item in Enum.GetValues(typeof(EnumStatus)) )
            {
                dictStatus.Add(item, item.ToString());
            }
            RefreshDataGrid();

            //lstDetails.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = Utility.GetAllClients(ServiceUrl) });
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            RefreshDataGrid();
        }

        private void btnAddNewTask_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            dlg.DefaultExt = ".ini";
            dlg.Filter = "Hash Task File (.ini)|*.ini";

            Nullable<bool> result = dlg.ShowDialog();

            if (result==true)
            {
                string filename = dlg.FileName;
                //MessageBox.Show(filename);

                

                string rawImei;
                string rawSha;
                int count = 0;
                int err = 0;
                using (StreamReader reader = new StreamReader(new FileStream(filename, FileMode.Open)))
                {
                    while (!reader.EndOfStream)
                    {
                        rawImei = reader.ReadLine();
                        if (rawImei.Length == 0) continue;
                        if (rawImei.Substring(0, 1) == "[" && rawImei.Substring(rawImei.Length - 1, 1) == "]")
                        {
                            rawImei = rawImei.Substring(1, rawImei.Length - 2);
                            //rawImei = "00" + rawImei + "00";
                            rawSha = reader.ReadLine();
                            rawSha = rawSha.Substring(4);

                            bool ret = ServiceUtility.AddNewTask(rawImei, rawSha, 5, ServiceUrl);

                            if (ret==true)
                            {
                                count++;
                            }
                            else
                            {
                                err++;
                            }

                        }
                    }
                }
                //File.Delete(filename);
                MessageBox.Show("新增 " + count.ToString() + "个任务，" + err.ToString() + " 个重复");


            }

        }

        private void dataDetails_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {

            
            Task currentTask=(Task)dataDetails.CurrentItem;
            //DataGrid grid = (DataGrid)sender;
            //grid.CommitEdit(DataGridEditingUnit.Row, true);

            TextBox t = e.EditingElement as TextBox;

            ServiceUtility.ModifyTask( ServiceUrl,currentTask.Imei,memo:t.Text);
            dataDetails.CommitEdit();
            RefreshDataGrid();
            //MessageBox.Show("dsf");
        }


        private void comboPriority_DropDownClosed(object sender, EventArgs e)
        {
            Task currentTask = (Task)dataDetails.CurrentItem;
            ComboBox cb=(ComboBox)sender;

            ServiceUtility.ModifyTask(ServiceUrl,currentTask.Imei, int.Parse(cb.SelectedValue.ToString()) );

            RefreshDataGrid();

        }

        private void mnuPriority3_Click(object sender, RoutedEventArgs e)
        {
            foreach (Task item in dataDetails.SelectedItems)
            {
                //MessageBox.Show(item.Imei);
                ServiceUtility.ModifyTask(ServiceUrl, item.Imei, 3);
            }
            RefreshDataGrid();
        }

        private void mnuPriority2_Click(object sender, RoutedEventArgs e)
        {
            foreach (Task item in dataDetails.SelectedItems)
            {
                //MessageBox.Show(item.Imei);
                ServiceUtility.ModifyTask(ServiceUrl, item.Imei, 2);
            }
            RefreshDataGrid();
        }

        private void mnuPriority1_Click(object sender, RoutedEventArgs e)
        {
            foreach (Task item in dataDetails.SelectedItems)
            {
                //MessageBox.Show(item.Imei);
                ServiceUtility.ModifyTask(ServiceUrl, item.Imei, 1);
            }
            RefreshDataGrid();
        }

        private void mnuPriority0_Click(object sender, RoutedEventArgs e)
        {
            foreach (Task item in dataDetails.SelectedItems)
            {
                //MessageBox.Show(item.Imei);
                ServiceUtility.ModifyTask(ServiceUrl, item.Imei, 0);
            }
            RefreshDataGrid();
        }

        private void chkMode_Checked(object sender, RoutedEventArgs e)
        {
            
        }

        private void chkMode_Click(object sender, RoutedEventArgs e)
        {
            RefreshDataGrid();

        }
    }
}
